<?php
namespace app\admin\controller;
use app\admin\model\Account as AccountModel;
use app\admin\model\LessonComment;
use app\admin\model\Order;
use app\admin\model\User;
use think\Controller;
use think\Db;
use think\Validate;
use think\Log;
class Home extends UserAuth
{
    //到课(学生出勤)情况处理
    public function attend_lesson(){
        //if(request()->isAjax()){
        $_POST['lesson_id']=1;
        $_POST['user_id']=json_encode(array(10,12,5));
            $id = $_POST['lesson_id'];
            $user_id = json_decode($_POST['user_id']);
            $info=Db::name('lesson')->where(array('id'=>$id))->find();
            $names=Db::name('lesson_comment')->where(array('lesson_id'=>$_POST['lesson_id']))->select();
            // 制作评论表里 当前课时数
            $comments=new LessonComment();
            $orders=new Order();
            foreach($names as $k1=>$v1){
                $new_data['complete_lesson_num']=$names[$k1]['complete_lesson_num']+1;
                $comments->where(array('lesson_id'=>$id))->update($new_data);
            }
            $lesson_data['com_lesson_num']=$new_data['complete_lesson_num'];
            Db::name('lesson')->where('id='.$id)->update($lesson_data);
            $order_name=Db::name('order')->where(array('lesson_id'=>$id))->select();
            //dump($order_name);die;
            // 制作Order表里的当前课时数
            foreach($order_name as $k2=>$v2){
                $ord['complete_lesson_num']=$names[$k2]['complete_lesson_num']+1;
                $orders->where(array('lesson_id'=>$id))->update($ord);
            }
            //制作Order表里逃课数
            if($info['cost_type'] == 1){
                $where['lesson_id'] = $_POST['lesson_id'];
                $where['comment_rank'] = array('elt',3);
                $comment_name = Db::name('lesson_comment')->where($where)->select();
                foreach($comment_name as $k3=>$v3){
                    $arr[]=$comment_name[$k3]['comment_id'];
                }
            }elseif($info['cost_type'] == 2){
                foreach($order_name as $k3=>$v3){
                    $arr[]=$order_name[$k3]['user_id'];
                }
            }
            $diff = array_diff($arr,$user_id);
            if($diff){
                foreach($diff as $k4=>$v4){
                    $escape=Db::name('order')->where(array('lesson_id'=>$id,'user_id'=>$v4))->find();
                    $escape['escape_lesson_num']=$escape['escape_lesson_num']+1;
                    $orders->where(array('lesson_id'=>$id,'user_id'=>$diff[$k4]))->update($escape);
                }
            }else{

            }
            // 制作 comment表的当前出席人数
            foreach($user_id as $k5=>$v5){
                $one_comment=Db::name('lesson_comment')->where(array('lesson_id'=>$id,'comment_id'=>$user_id[$k5]))->find();
                $data['attend_lesson_num']= $one_comment['attend_lesson_num']+1;
                $comments->where(array('lesson_id'=>$id,'comment_id'=>$v5))->update($data);
            }
            return json(0);
       // }

    }
    //  教师端 确认当前课时结束
    public function end_lesson_now(){
        $status0['status']=0;
        $status1['status']=1;
        $status2['status']=2;
       // if(request()->isPost()) {
        $_POST['lesson_id']=1;
        $_POST['comment_lesson_number']=12;
            $lesson = Db::name('lesson')->where(array('id' => $_POST['lesson_id']))->find();
            if ($lesson['is_comment'] == 1) {
                $lesson_num = $lesson['lesson_num'];
                $comment = Db::name('lesson_comment')->where(array('lesson_id' => $_POST['lesson_id']))->select();
                if (!($lesson_num - $_POST['comment_lesson_number'] == 0 || $lesson_num - $_POST['comment_lesson_number'] == 1)) {
                    foreach ($comment as $k => $v) {
                        if ($_POST['comment_lesson_number'] !=$comment[$k]['attend_lesson_num']) {
                            $comment[$k]['attend_lesson_num'] = $_POST['comment_lesson_number'];//将所接收的课时数替换order表中的attend_lesson_num的课时数
                            Db::name('lesson_comment')->where(array('comment_id' => $comment[$k]['comment_id'], 'lesson_id' => $comment[$k]['lesson_id']))->update($comment[$k]);

                            if ($comment[$k]['comment_rank'] <= 3) {
                                $comment[$k]['comment_status'] = 0;
                                $comment[$k]['comment_lesson_number'] = $_POST['comment_lesson_number'];
                                $comment[$k]['comment_start_time']=time();
                                $rec = Db::name('lesson_comment')->where(array('comment_id' => $comment[$k]['comment_id'], 'lesson_id' => $comment[$k]['lesson_id']))->update($comment[$k]);

                                if ($rec) {
                                    // echo '<script>alert(\'1，成功修改，偶数，改状态，改课时数\');</script>';
                                } else {
                                    //echo '<script>alert(0);</script>';
                                }
                            } else {

                            }
                        } else {
                            if ($comment[$k]['comment_rank'] <= 3) {
                                $comment[$k]['comment_lesson_number'] = $_POST['comment_lesson_number'];
                                $comment[$k]['comment_status'] = 1;
                                $comment[$k]['attend_lesson_num'] = $_POST['comment_lesson_number'];
                                $comment[$k]['comment_start_time']=time();
                                Db::name('lesson_comment')->where(array('comment_id' => $comment[$k]['comment_id'], 'lesson_id' => $comment[$k]['lesson_id']))->update($comment[$k]);
                            } else {

                            }

                        }


                    }
                    return json($status0);
                } else { // 默认评价的

                    foreach ($comment as $k => $v) {
                        if ($_POST['comment_lesson_number'] !=$comment[$k]['attend_lesson_num']) {
                            $comment[$k]['attend_lesson_num'] = $_POST['comment_lesson_number'];//将所接收的课时数替换order表中的attend_lesson_num的课时数

                            Db::name('lesson_comment')->where(array('comment_id' => $comment[$k]['comment_id'], 'lesson_id' => $comment[$k]['lesson_id']))->update($comment[$k]);

                            if ($comment[$k]['comment_rank'] <= 3) {
                                $comment[$k]['comment_lesson_number'] = $_POST['comment_lesson_number'];
                                $comment[$k]['comment_start_time']=time();
                                $rec = Db::name('lesson_comment')->where(array('comment_id' => $comment[$k]['comment_id'], 'lesson_id' => $comment[$k]['lesson_id']))->update($comment[$k]);

                                if ($rec) {
                                    //echo '<script>alert(\'1，成功修改，偶数，改状态，改课时数\');</script>';
                                } else {
                                    // echo '<script>alert(0);</script>';
                                }
                            } else {

                            }
                        } else {
                            if ($comment[$k]['comment_rank'] <= 3) {
                                $comment[$k]['comment_lesson_number'] = $_POST['comment_lesson_number'];
                                $comment[$k]['comment_status'] = 1;
                                $comment[$k]['attend_lesson_num'] = $_POST['comment_lesson_number'];
                                Db::name('lesson_comment')->where(array('comment_id' => $comment[$k]['comment_id'], 'lesson_id' => $comment[$k]['lesson_id']))->update($comment[$k]);
                            } else {

                            }
                        }


                    }
                    return json($status0);
                    //echo '<script>alert(\'本课不发起评价\')</script>';
                }
            } else {  //不需要评论的
                $names_c = Db::name('lesson_comment')->where(array('lesson_id' => $_POST['lesson_id']))->select();

                //dump($names_c);die;
                foreach ($names_c as $k1 => $v1) {
                    $names_c[$k1]['comment_lesson_number'] = $_POST['comment_lesson_number'];
                    $n_c = Db::name('lesson_comment')->where(array('lesson_id' => $_POST['lesson_id'], 'comment_id' => $names_c[$k1]['comment_id']))->update($names_c[$k1]);
                }
                if ($n_c) {
                    return json($status0);
                    //echo  '<script>alert(1);</script>';
                } else {
                    return json($status1);
                    // echo  '<script>alert(\'no_c 失败\');</script>';
                }
            }
      //  }
    }
}